<template>
  <view style="width: 100%; height: 100%"><l-echart :ref="chartRef" @finished="initChart"></l-echart></view>
</template>

<script>
import * as echarts from '@/uni_modules/lime-echart/static/echarts.min';

export default {
  name:"line-chart",
  props:{
    chartRef:{
      type:String,
      default:'chartRef'
    },
    chartData:{
      type:Object,
      default:()=>{
        return {
          legendData: [],
          dataX: [],
          dataY: [],
          colorList: []
        }
      }
    },
    interval:{
      type:Number,
      default:2
    },
    gridLeft:{
      type:[String,Number],
      default:'2%'
    },
    gridRight:{
      type:[String,Number],
      default:'2%'
    },
    dataZoom:{
      type:Boolean,
      default:false
    }
  },
  watch: {
    chartData: {
      handler(val) {
        this.initChart();
      },
      deep: true
    }
  },
  data() {
    return {

    };
  },
  mounted() {
  
  },
  methods: {
    async initChart(){
      let seriesData = this.chartData.dataY.map((item,inx) => {
        return {
          name: this.chartData.legendData[inx],
          type: 'line',
          smooth: true,
          showSymbol: false,
          data: item,
          itemStyle: {
            color: this.chartData.colorList[inx].line
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: this.chartData.colorList[inx].area0
              },
              {
                offset: 1,
                color: this.chartData.colorList[inx].area1
              }
            ])
          }
        };
      });
      let option = {
        tooltip: {
          trigger: 'axis'
        },
        grid: {
          left: this.gridLeft,
          right: this.gridRight,
          bottom: '2%',
          top: '12%',
          containLabel: true
        },
        legend: {
          right: 0,
          itemGap: 16,
          itemWidth: 8,
          itemHeight: 8,
          icon: 'rect',
          data: this.chartData.legendData,
          textStyle: {
            color: 'rgba(16, 44, 89, 0.5)',
            fontSize: 12
          }
        },
        xAxis: [
          {
            type: 'category',
            data: this.chartData.dataX,
            boundaryGap: false,
            axisLabel: {
              textStyle: {
                fontSize: 12,
                color: '#102C59'
              },
              alignMaxLabel:'center',
              interval: this.interval //间隔
            },
            axisLine: {
              show: true,
              lineStyle: {
                type: 'dashed',
                color: 'rgba(0, 0, 0, 0.3)'
              }
            },
            axisTick: {
              show: false //小刻度
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            nameTextStyle: {
              color: 'rgba(16, 44, 89, 1)'
            },
            minInterval: 1,
            axisLabel: {
              show: true,
              textStyle: {
                color: '#102C59',
                fontSize: 12
              },
              formatter: '{value}'
            },
            // splitNumber: 4,
            splitLine: {
              //网格线
              lineStyle: {
                show: true,
                type: 'dashed',
                opacity: 1,
                width: 1,
                color: 'rgba(0, 0, 0, 0.1)'
              }
            },
            axisLine: {
              show: false
            }
          }
        ],
        dataZoom: !this.dataZoom?[]:[
          {
            type: 'slider',
            show: false,
            startValue: this.chartData.dataX.length - 5,
            endValue: this.chartData.dataX - 1
          },
          {
            type: 'inside',
            startValue: this.chartData.dataX.length - 5,
            endValue: this.chartData.dataX - 1
          }
        ],
        series: seriesData
      };
    
      const chart = await this.$refs[this.chartRef].init(echarts);
      chart.setOption(option);
    }
  }
  
}
</script>

<style></style>
